window.addEventListener('load', function () {
    let file = document.querySelector('#file')
    let image = document.querySelector('#image')
    //事件代理
    btnChooseImage.addEventListener('click', function () {
        file.click()
    })
    file.addEventListener('change', function (e) {
        if (this.files.length === 0) return layer.msg('请选择文件')
        let imgUrl = URL.createObjectURL(this.files[0])
        cropper.replace(imgUrl)
    })
    const cropper = new Cropper(image, {
        aspectRatio: 1,
        preview: '.img-preview'
    });
    //上传到网络
    btnUpload.addEventListener('click', function () {
        let str = cropper.getCroppedCanvas({
            width: 100,
            heigth: 100
        }).toDataURL('image/png')
        str = encodeURIComponent(str)
        obj.url = '/my/update/avatar'
        obj.type = 'post'
        obj.data = { avatar: str }
        obj.success = function (res) {
            if (res.status === 0) {
                layer.msg(res.message)
                setTimeout(() => window.parent.location.assign('/index.html'), 1000)
            }
        }
        myAxios(obj)
    })
})